<?php
require($_SERVER['DOCUMENT_ROOT'].'/cofd/common.php');
$resultData = [
    "code" => 0,
    "msg" => "",
    "count" => 0,
    "data" => []
];
function gbToMb($gb) {
    $mb = $gb * 1024;
    return $mb;
}
try {
    $sql = "SELECT id, names, pwd, froms, timei,sizes FROM files";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $result = $stmt->get_result();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $fileid = $row['id'];
            $filename = $row['names'];
            $filepwd = $row['pwd']; 
            $filefrom = $row['froms'];
            $filetime = $row['timei'];
            $resultData['data'][] = [
                "fileid" => $fileid,
                "filename" => $filename,
                "filepwd" => $filepwd,
                "filefrom" => $filefrom,
                "filetime" => $filetime,
                "filesize" => gbToMb($row['sizes'])
            ];
            $resultData['count']++;
        }
    } else {
        $resultData['msg'] = "未找到文件数据";
    }
} catch (Exception $e) {
    $resultData['code'] = 1;
    $resultData['msg'] = "查询失败：" . $e->getMessage();
}
echo json_encode($resultData, JSON_UNESCAPED_UNICODE);